package com.xtt.proxyIp;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * @author ct
 * @description 获取高匿代理ip
 * @createTime 2017年4月3日 下午5:08:20
 */
public class ProxyIp {
	private static QueueList<HttpHost> queue = new QueueList<HttpHost>();

	private static final String requestUrl = "http://www.xicidaili.com/nn";
	
	public static void grapIp() {
		try {
			Document doc = Jsoup.parse(getResponseString());
			Elements ips = doc.select("#ip_list tr[class] td:eq(1)");
			Elements ports = doc.select("#ip_list tr[class] td:eq(2)");
			Elements speed = doc.select("#ip_list tr[class] td:eq(6) div[title]");
			for(int i = 0; i < ips.size(); i++) {
				if(Double.parseDouble(speed.get(i).attr("title").replace("秒", "")) < 2) {//连接速度小于2s
					System.out.println("ip:" + ips.get(i).text() + " || port:" + ports.get(i).text() + "  || speed:" + speed.get(i).attr("title"));
					HttpHost host = new HttpHost(ips.get(i).text(), Integer.parseInt(ports.get(i).text()));
					queue.offer(host);
					if(queue.size() == 40) break;
				}
			}
			System.out.println("==============");
			System.out.println(queue.size());
		} catch (Exception e) {
			throw new RuntimeException("解析失败",e);
		}
	}
	
	private static String getResponseString() throws ClientProtocolException, IOException {
		CloseableHttpClient client = HttpClients.createDefault();
		HttpGet get = new HttpGet(requestUrl);
		get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64) "
				+ "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");//模拟google
		HttpHost proxy = getHost();
		RequestConfig config = RequestConfig.custom()
				.setConnectionRequestTimeout(10000)
				.setConnectTimeout(10000)
				.setSocketTimeout(10000)
				.setProxy(proxy)
				.build();
		get.setConfig(config);
		CloseableHttpResponse response = client.execute(get);
		HttpEntity entity = response.getEntity();
		return EntityUtils.toString(entity);
	}
	
	
	public static HttpHost getHost() {
		return queue.poll();
	}
	public static void setHost(HttpHost host) {
		queue.offer(host);
	}
}
